<template>
    <Card>
        <p slot="title">运营人员管理列表</p>
        <div class="search-con search-con-top">
            昵称：
            <Input v-model="searchData.nick" style="width: 200px"></Input>
            账号：
            <Input v-model="searchData.username" style="width: 200px"></Input>
            手机号：
            <Input v-model="searchData.phone" style="width: 200px"></Input>
            <Button @click="search" type="primary" shape="circle" icon="ios-search"
                style="margin-left: 10px;">搜索</Button>
            <Button @click="reset" type="success" shape="circle" icon="ios-refresh"
                style="margin-left: 10px;">刷新</Button>
        </div>
        <br>

        <Table :columns="columns" :data="list" border>
            <template slot-scope="{ row , index}" slot="makeSlot">
                <i-button type="primary" size="small" @click="edit(row)">编辑</i-button>
            </template>
        </Table>
        <br>
        <Page :total="total"></Page>
        <!-- 添加管理员抽屉 -->
        <Drawer title="添加账号" :closable="false" width="40" v-model="showAdd" @on-close="drawClose">
            <Form ref="formValidate" :model="formItem" :label-width="100">
                <FormItem label="人员信息">
                    {{formItem.nick}}&nbsp;&nbsp;&nbsp;&nbsp;{{formItem.phone}}
                </FormItem>
                <FormItem label="所属运营商" prop="operator_id" >
                    <Select v-model="formItem.operator_id" filterable>
                        <Option v-for="item in operatorList" :value="item.id" :key="item.id">{{ item.name }}</Option>
                    </Select>
                </FormItem>
                <FormItem label="所属省">
                    <Select v-model="formItem.province_id" filterable @on-change="provinceListChange" :disabled="hiddenProvinceSelect">
                        <Option v-for="item in provinceList" :value="item.id" :key="item.id">{{ item.name }}</Option>
                    </Select>
                </FormItem>
                <FormItem label="所属市">
                    <Select v-model="formItem.city_id" filterable @on-change="cityListChange" :disabled="hiddenCitySelect">
                        <Option v-for="item in cityList" :value="item.id" :key="item.id">{{ item.name }}</Option>
                    </Select>
                </FormItem>
                <FormItem label="所属区县">
                    <CheckboxGroup v-model="formItem.area_id">
                        <Checkbox v-for="item in areaList" :value="item.id" :label="item.id">{{item.name}}</Checkbox>
                    </CheckboxGroup>
                </FormItem>
                <FormItem>
                    <Button type="primary" @click="editOperator()">提交</Button>
                </FormItem>
            </Form>
        </Drawer>
    </Card>
</template>

<script>


export default {
    data() {
        return {
            searchData: {
                page: 1,
                limit: 10
            },
            // 列表
            columns: [
                {
                    title: 'ID',
                    key: 'id',
                    minWidth: 50,
                    maxWidth: 100,
                },
                {
                    title: '运营人员',
                    key: 'nick',
                    minWidth: 100,
                },
                {
                    title: '手机号',
                    key: 'phone',
                    minWidth: 100,
                },
                {
                    title: '所属运营商',
                    key: 'operator_name',
                    minWidth: 100,
                },
                {
                    title: '所属地区',
                    key: 'area_name',
                    minWidth: 100
                },
                {
                    title: '操作',
                    slot: 'makeSlot',
                    minWidth: 100,
                },
            ],
            // 数据
            list: [],
            total: 0,
            showAdd: false,
            delUserId: '',
            hiddenProvinceSelect:false,
            hiddenCitySelect:false,
            // 管理信息
            formItem: {
                id: 0,
                nick: '',
                phone: '',
                province_id:0,
                city_id:0,
                area_id:[],
            },
            //运营商列表
            operatorList: [],
            // 角色列表
            roleList: [],
            //省列表
            provinceList: [],
            //市列表
            cityList: [],
            //区县列表
            areaList: [],
        }
    },
    methods: {
        search() {
            this.searchData.page = 1;
            this.getList();
        },
        reset() {
            this.searchData = {
                page: 1,
                limit: 10,
            };
            this.getList();
        },
        // 查询管理员信息
        getList() {
            let params = {
                ...this.searchData,
            };
            this.$axios('/adminuser/getOperatorUserList', params).then(res => {
                this.list = res;
            });
        },
        //打开添加管理员抽屉 获取角色列表
        showAddDrawer() {
            this.showAdd = true;
            this.getOperatorList();
            this.getProvinceList();
        },
        // 绑定运营地区
        editOperator() {
            this.$axios('/adminuser/saveOperatorUserArea', this.formItem).then(res => {
                this.$Message.success("绑定成功");
                this.showAdd = false;
                this.getList();
            });
        },
        drawClose() {
            this.showAdd = false;
        },
        //省列表
        getProvinceList() {
            this.$axios('/adminregion/getProvinceList', {}).then(res => {
                this.provinceList = res;
            });
        },
        //省列表切换
        provinceListChange(value) {
            this.$axios('/adminregion/getCityList', { province_id: value }).then(res => {
                this.cityList = res;
            });
        },
        //市列表切换
        cityListChange(value) {
            this.city_id = value;
            this.$axios('/adminregion/getAreaList', { city_id: value }).then(res => {
                this.areaList = res;
            });
        },
        // 启用，禁用
        statusChange(id, status) {
            this.$axios('/adminuser/status', { id: id, status: status }).then(res => {
                this.getAdminUserList();
            });
        },
        // 编辑
        edit(info) {
            this.formItem.id = info.id;
            this.formItem.nick = info.nick;
            this.formItem.phone = info.phone;
            this.formItem.province_id = info.province_id;
            this.formItem.city_id = info.city_id;
            this.formItem.area_id = info.area_id;
            this.formItem.operator_id = info.operator_id;
            this.formItem.operator_name = info.operator_name;
            if(info.province_id >0 ){
                this.hiddenProvinceSelect = true;
            }
            if(info.city_id >0 ){
                this.cityList = [{ id: info.city_id, name: info.city_name }];
                this.hiddenCitySelect = true;
                this.$axios('/adminregion/getAreaList', { city_id: info.city_id }).then(res => {
                    this.areaList = res;
                });
            }
            this.showAddDrawer()
        },
        //运营商户列表
        getOperatorList() {
            this.$axios('/adminstatistics/getOperatorList', {}).then(res => {
                this.operatorList = res;
            });
        },
    },
    created() {
        this.getList();
    }
}
</script>
<style>

</style>
